package com.example.customviewapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class CircleProgressView extends View {


    private int mMeasureHeight;

    private int mMeasureWidth;

    private float mCircleXY;

    private float mRadius;

    private Paint mCirclePaint;

    private RectF mArcRectF;

    private float mSweepAngle;

    private float mSweepValue = 66;

    private Paint mArcPaint;

    private String mShowText;

    private float mShowTextSize;

    private Paint mTextPaint;

    public CircleProgressView(Context context, AttributeSet attributeSet,
                              int defStyleAttr){
        super(context,attributeSet,defStyleAttr);

    }

    public CircleProgressView(Context context,AttributeSet attributeSet){
        super(context,attributeSet);
    }

    public CircleProgressView(Context context){
        super(context);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mMeasureHeight = MeasureSpec.getSize(heightMeasureSpec);
        mMeasureWidth = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(mMeasureWidth,mMeasureHeight);
        initView();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(mCircleXY,mCircleXY,mRadius,mCirclePaint);
        canvas.drawArc(mArcRectF,270,mSweepAngle,false,mArcPaint);
        canvas.drawText(mShowText,0,mShowText.length(),
                mCircleXY,mCircleXY+(mShowTextSize / 4),mTextPaint);
    }

    private void initView(){
        float length = 0;
        if(mMeasureHeight>=mMeasureWidth){
            length = mMeasureWidth;
        }else {
            length = mMeasureHeight;
        }

        mCircleXY = length / 2;
        mRadius = (float) (length * 0.5 /2);
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(getResources().getColor(android.R.color.holo_blue_bright));
        mArcRectF = new RectF(
                (float)(length * 0.1),
                (float)(length * 0.1),
                (float)(length * 0.9),
                (float)(length * 0.9));

        mSweepAngle = (mSweepValue /100f) * 360f;
        mArcPaint = new Paint();
        mArcPaint.setAntiAlias(true);
        mArcPaint.setColor(getResources().getColor(android.R.color.holo_blue_bright));
        mArcPaint.setStrokeWidth((float)(length * 0.1));
        mArcPaint.setStyle(Paint.Style.STROKE);


        mShowText = setShowText();
        mShowTextSize = setShowTextSize();

        mTextPaint = new Paint();
        mTextPaint.setTextSize(mShowTextSize);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }


    private String setShowText(){
        this.invalidate();
        return "Android Skill";
    }

    private float setShowTextSize(){
        this.invalidate();
        return 50;
    }
}
